package cn.sciento.export;

import java.util.HashMap;
import java.util.Map;

import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class ExcelFillerHolder implements ApplicationContextAware {
    private static final Map<String, ExcelFiller> FILLER_HOLDER = new HashMap<>();

    private ApplicationContext context;

    public static ExcelFiller getExcelFiller(String fillerType, SXSSFWorkbook workbook) {
        ExcelFiller filler = FILLER_HOLDER.get(fillerType);
        try {
            return filler.getClass().getConstructor(new Class[]{SXSSFWorkbook.class}).newInstance(workbook);
        } catch (Exception e) {
            return null;
        }
    }

    public void init() {
        Map<String, ExcelFiller> beans = this.context.getBeansOfType(ExcelFiller.class);
        beans.forEach((k, filler) -> FILLER_HOLDER.put(filler.getFillerType(), filler));
    }

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.context = applicationContext;
    }
}
